package com.alks.function.controller.finance.fincons;

import com.alks.common.aop.ApiRestController;
import com.alks.common.aop.RoleCheck;
import com.alks.common.aop.WebLogger;
import com.alks.common.utils.excelUtils.ExcelUtils;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.entity.exception.ServiceErrorException;
import com.alks.function.data.dto.finance.fincons.InboundDetailsDto;
import com.alks.function.data.request.finance.ryfinance.MtlPreOtherInRequest;
import com.alks.function.service.finance.fincons.ReceiptReconciliationService;
import com.alks.function.service.finance.mtlcosts.IMcRecTbAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Slf4j
@Api(value = "财务部-工厂-财务咨询查询", tags = {"财务部-收货对账查询"})
@ApiRestController("/ReceiptReconciliation")
@RoleCheck(prgIds = {"100501","180501"})
public class ReceiptReconciliationController {

    @Autowired
    private ReceiptReconciliationService receiptReconciliationService;

    @Autowired
    private IMcRecTbAccountService service;

    @WebLogger
    @PostMapping("/mtlPreOtherInQuery")
    @ApiOperation(value = "入库明细查询",tags = "财务部-收货对账查询")
    public ResponseInfo mtlPreOtherInQuery(@RequestBody MtlPreOtherInRequest request){
        Map<String, Object> map = service.mtlPreOtherInQuery(request);
        return  ResponseInfo.ok(map);
    }

    @WebLogger
    @PostMapping("/exportInboundDetails")
    @ApiOperation(value = "导出明细",tags = "财务部-收货对账查询")
    public void exportInboundDetails(@RequestBody MtlPreOtherInRequest request, HttpServletResponse response) throws IOException {
        //receiptReconciliationService.exportInboundDetails(request,response);
        request.setPageSize(-1);
        String templet = "收货对账作业_1模版.xlsx";
        String fileName = "收货对账作业_1" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyMMdd")) + ".xlsx";
        Map<String, Object> map = service.mtlPreOtherInQuery(request);
        List<InboundDetailsDto> dtoList = (List<InboundDetailsDto>) map.get("data");
        BigDecimal amtSum = dtoList.stream().map(InboundDetailsDto::getAmt).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
        InboundDetailsDto dto = new InboundDetailsDto();
        dto.setAmt(amtSum);
        dto.setSeqName("合计");
        dtoList.add(dto);
        try {
            ExcelUtils.creatByTemplate(response, templet, fileName, dtoList);
        } catch (Exception e) {
            throw new ServiceErrorException("导出失败");
        }
    }

    @WebLogger
    @PostMapping("/queryDailyBill")
    @ApiOperation(value = "查询当日单据数量",tags = "财务部-收货对账查询")
    public ResponseInfo queryDailyBill(){
        return receiptReconciliationService.queryDailyBill();
    }
}
